import fetch from 'utils/fetch'

// 获取定制列表
export function getTasks(data) {
  return fetch({
    url: '/task/',
    method: 'get',
    params: data
  })
}

// 发布私人定制
export function publishTask(data) {
  // console.log(data)
  return fetch({
    url: '/task/publish/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      const form = new FormData()
      for (const key in data) {
        form.append(key, data[key])
      }
      return form
    }],
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取定制详情
export function getTask(data) {
  return fetch({
    url: '/task/info/',
    method: 'get',
    params: data
  })
}

// 我要接单
export function takeTask(data) {
  return fetch({
    url: '/task/take/',
    params: data,
    method: 'get'
  })
}

// 上传稿件
export function uploadTask(data) {
  return fetch({
    url: '/task/upload/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      const form = new FormData()
      for (const key in data) {
        form.append(key, data[key])
      }
      return form
    }],
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// /task/upload/
export function deleteTask(data) {
  return fetch({
    url: '/task/upload/',
    method: 'delete',
    params: data
  })
}

export function closeTask(data) {
  return fetch({
    url: '/task/close/',
    method: 'get',
    params: data
  })
}

// /task/info/
export function chooseTask(data) {
  return fetch({
    url: '/task/info/',
    method: 'post',
    data,
    // headers: {
    //   'Content-Type': 'application/json'
    // }
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (const it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}